﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using StableDiffusionTools.Enums;
using StableDiffusionTools.Framework;

namespace StableDiffusionTools.Forms
{
    public partial class MultiLineInputDialogWithEnum<TEnum> : KryptonForm
    {
        public List<string> Result { get; private set; } = new List<string>();
        public TEnum ResultEnum { get; private set; } = default(TEnum);
        public bool AllowEmpty;

        public MultiLineInputDialogWithEnum(string text, string caption = "", List<string> inputDefault = null, bool allowEmpty = false, string enumText = "", TEnum @enum = default(TEnum), bool selectText = false) : this(text, caption, inputDefault != null ? string.Join(ControlChars.CrLf, inputDefault) : null, allowEmpty, enumText, @enum, selectText) { }
        public MultiLineInputDialogWithEnum(string text, string caption = "", string inputDefault = null, bool allowEmpty = false, string enumText = "", TEnum @enum = default(TEnum), bool selectText = false)
        {
            InitializeComponent();

            Size newSize = TextRenderer.MeasureText(text, Font, new Size(1000, 1000), TextFormatFlags.Default);
            ClientSize = new Size(Math.Max(ClientSize.Width, newSize.Width), newSize.Height + ClientSize.Height);
            kryptonLBLHeader.Text = text;
            if (!string.IsNullOrEmpty(caption))
                Text = caption;

            kryptonLBLEnumHeader.Text = enumText;
            //enumComboBox1.PopulateFromEnum(@enum);
            TEnum[] enums = EnumUtil.GetValues<TEnum>();
            for (int i = 0; i < enums.Length; i++)
            {
                kryptonCMBEnums.Items.Add(enums[i]);
            }

            kryptonCMBEnums.SelectedItem = @enum;

            AllowEmpty = allowEmpty;

            kryptonTBInput.Text = !string.IsNullOrEmpty(inputDefault) ? inputDefault : "";

            if (selectText)
                kryptonTBInput.SelectAll();
            else
            {
                kryptonTBInput.Select(kryptonTBInput.Text.Length, 0);
            }

            ValidateEmpty();

            KeyDown += (s, e) =>
            {
                if (e.KeyCode == Keys.Escape)
                {
                    DialogResult = DialogResult.Cancel;
                }
                else if((e.Alt || e.Shift || e.Control) && e.KeyCode == Keys.Enter && (AllowEmpty ? true : !string.IsNullOrWhiteSpace(kryptonTBInput.Text)))
                {
                    UpdateResult();
                    DialogResult = DialogResult.OK;
                }
            };

            StartPosition = FormStartPosition.CenterParent;
        }

        private void kryptonBTNOk_Click(object sender, EventArgs e)
        {
            UpdateResult();
        }

        private void kryptonBTNClear_Click(object sender, EventArgs e)
        {
            kryptonTBInput.Text = "";
        }

        private void kryptonTBInput_TextChanged(object sender, EventArgs e)
        {
            ValidateEmpty();
        }

        private void ValidateEmpty()
        {
            bool okEnabled = AllowEmpty ? true : !string.IsNullOrWhiteSpace(kryptonTBInput.Text);
            kryptonBTNOk.Enabled = okEnabled;
        }

        private void UpdateResult()
        {
            if (kryptonTBInput.Lines != null)
                Result = new List<string>(kryptonTBInput.Lines.Where((s) => !string.IsNullOrEmpty(s)));

            ResultEnum = (TEnum)kryptonCMBEnums.SelectedItem;
        }

        [Obsolete("This is not supported in this class.", true)]
        public new void Show(IWin32Window window) { }
        [Obsolete("This is not supported in this class.", true)]
        public new void Show() { }
    }
}
